// Function to switch an entry between 2 lists given in parameter
function switch_list(idleft, idright){
	for (i = 0; i < document.getElementById(idleft).length; i++)
	{
		if (document.getElementById(idleft).options[i].selected == true)
		{
			new_elt = new Option(document.getElementById(idleft).options[i].text, document.getElementById(idleft).options[i].value);
			document.getElementById(idright).options[document.getElementById(idright).length] = new_elt;
			document.getElementById(idleft).options[i] = null;
			i--;
		}
	}
	for (i = 0; i < document.getElementById('SmileRight').length; i++)
		document.getElementById('SmileRight').options[i].selected = true;
	for (i = 0; i < document.getElementById('ClientRight').length; i++)
		document.getElementById('ClientRight').options[i].selected = true;
}

function update_client_list(id){
	// We have to reinitialize the lists.
	for (i = document.getElementById("ClientLeft").length; i > 0; i--)
		document.getElementById("ClientLeft").options[i - 1] = null;		
	for (i = document.getElementById("ClientRight").length; i > 0; i--)
		document.getElementById("ClientRight").options[i - 1] = null;
	for (i = document.getElementById("ClientCP").length; i > 0; i--)
		document.getElementById("ClientCP").options[i - 1] = null;	
		
	company_id = 0;
	// We get the company_id from Contract
	for (i = 0; i < contractList.length; i++)
	{
		if (contractList[i]['id'] == id)
			company_id = contractList[i]['company_id'];
	}
	
	// We fill the left list with the selected company users.
	for (i = 0; i < usrList.length; i++)
	{
		if (usrList[i]['company_id'] == company_id)
		{
			str = "" + usrList[i]['lastname'] +" "+ usrList[i]['firstname'];
			new_elt = new Option(str, usrList[i]['id']);
			new_elt2 = new Option(str, usrList[i]['id']);
			document.getElementById("ClientLeft").options[document.getElementById("ClientLeft").length] = new_elt;
			document.getElementById("ClientCP").options[document.getElementById("ClientCP").length] = new_elt2;
		}
	}
}

// Delete the option given in params in the ID component.
function delete_option(id, option){
	if (option.value == "")
		return;
//		document.write("id: "+id+", option: "+option.value+" "+option.text);	
	length = document.getElementById(id).length;
	for (i=0; i < length; i++){
//		document.write("option.value: "+option.value+", i.value: "+document.getElementById(id).options[i].value);	
		if (document.getElementById(id).options && (document.getElementById(id).options[i].value == option.value)){
			for (j=i; j < length; j++)
				document.getElementById(id).options[j] = document.getElementById(id).options[j + 1];
			document.getElementById(id).options[length - 1] = null;
			return;
		}
	}
}

// Javascript function to compare 2 strings, return 0 if they
// are even, -1 if str1 < str2 and returns 1 if str1 > str2.
function str_compare(str1, str2){
	l1 = str1.length;
	l2 = str2.length;
	length = Math.min(l1, l2);
	
	for (i=0; i < length; i++){
		if (str1[i] < str2[i])
			return -1;
		if (str1[i] > str2[i])
			return 1;				
	}
	if (l1 == l2)
		return 0;
	if (l1 < l2)
		return 1;
	if (l1 > l2)
		return -1;
}

// Add the option given in params in the ID component.
function add_option(id, option){
	if (option.value == "")
		return;	
	length = document.getElementById(id).length;
	for (i=0; i < length; i++){
		if (document.getElementById(id).options[i].value == option.value)
			return;
		if (str_compare(document.getElementById(id).options[i].text, option.text) > 0)
			continue;
		if (str_compare(document.getElementById(id).options[i].text, option.text) < 0){
			tmp = document.getElementById(id).options[i];
			document.getElementById(id).options[i] = option;
			for (j=length; j > i + 1; j--)
				document.getElementById(id).options[j] = document.getElementById(id).options[j - 1];
			document.getElementById(id).options[i + 1] = tmp;
			return;
		}
	}
	document.getElementById(id).options[length] = option;
}

var oldDPsmile = null;
var oldCPsmile = null;
var oldCPclient = null;

function update_list(id, option){
	//document.write("update_list : id : "+id+" option.value, option.text: "+option.value+", "+option.text);
	if (id == "smileDP")
	{
		//document.write("dans le if (id == 'smileDP') oldDPsmile vaut: "+oldDPsmile+"\n");
		if (oldDPsmile){
			//document.write("dans le if (oldDPsmile)\n");
			add_option('smileCP', oldDPsmile);
			add_option('SmileLeft', oldDPsmile);
		}
		oldDPsmile = option;
		//document.write("update_list : id : "+id+" option.value, option.text: "+option.value+", "+option.text);
		document.getElementById('smileCP').remove(option);
		document.getElementById('SmileLeft').remove(option);
		document.getElementById('SmileRight').remove(option);
	}
	if (id == "smileCP")
	{
		if (oldCPsmile){
			add_option('SmileLeft', oldCPsmile);
		}
		oldCPsmile = option;
		document.getElementById('SmileLeft').remove(option);
		document.getElementById('SmileRight').remove(option);
	}
	if (id == "ClientCP")
	{
		if (oldCPclient){
			add_option('SmileLeft', oldCPclient);
		}
		oldCPclient = option;
		document.getElementById('ClientLeft').remove(option);
		document.getElementById('ClientRight').remove(option);
	}

}